require 'config/environment'

task :estimate_kanji_level do
  raise "IO Error: Please specify csv filename (UTF-8)" unless ENV.has_key? "doc"
  rs = ENV.has_key?("rs") ? ENV["rs"] : "\t"
  #
  require 'csv'
  CSV.open(ENV["doc"],"r",rs) do |row|
    raise "Format Error: column size should be 2 columns." if row.size != 2
    raise "Format Error: first column should not be empty." if row.first.blank?
    raise "Format Error: second column should be integer(>-1)." if /^\d+$/ !~ row[1]
    #
    level = (row[1].to_f + 1.0) / 5.0
    Dictionary.find(:all,:conditions =>["index ~ ?",row[0]]).each do |entry|
      next if entry.kanji_level != nil && entry.kanji_level <= level
      entry.update_attribute :kanji_level,level
    end
  end
  
end